package com.video.information.ui.home.video

import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.video.information.Const
import com.video.information.R
import com.video.information.data.model.Video
import com.video.information.extension.kv
import com.video.information.extension.load
import com.video.information.ui.common.holder.ItemVideoHolder
import com.video.information.ui.common.holder.RecyclerViewHelp
import com.video.information.ui.common.listener.OnItemVideoClickListener

class VideoAdapter() : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    var videoData: List<Video.RECORDSBean>? = null

    var onItemVideoClickListener: OnItemVideoClickListener? = null

    constructor(videoData: List<Video.RECORDSBean>) : this() {
        this.videoData = videoData
    }

    override fun getItemViewType(position: Int): Int {
        return Const.ITEM_VIDEO_HOLDER
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return RecyclerViewHelp.getViewHolder(parent, viewType)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        val vh = holder as ItemVideoHolder
        val videoEntity = videoData?.get(position)

        vh.tvTitle.text = videoEntity?.vtitle
        vh.tvAuthor.text = videoEntity?.author
        vh.tvComment.text = videoEntity?.comment_num.toString()
        vh.tvDz.text = videoEntity?.like_num.toString()
        vh.tvCollect.text = videoEntity?.collect_num.toString()

        videoEntity?.headurl?.let { vh.imgHeader.load(it, 25f) }
        videoEntity?.coverUrl?.let { vh.mThumb.load(it) }

        if (onItemVideoClickListener != null) {
            vh.mPlayerContainer.setOnClickListener {
                onItemVideoClickListener?.onItemChildClick(position)
            }
        }

        vh.imgCollect.setOnClickListener {
            val collectNum = vh.tvCollect.text.toString().toInt()
            if (kv.decodeBool(vh.tvAuthor.text.toString() + "collect", false)) {
                if (collectNum > 0) {
                    vh.tvCollect.text = "${collectNum.dec()}"
                    kv.encode(vh.tvAuthor.text.toString() + "collect", false)

                    vh.imgCollect.setImageResource(R.drawable.collect)
                }
            } else {
                vh.tvCollect.text = "${collectNum.inc()}"
                kv.encode(vh.tvAuthor.text.toString() + "collect", true)
                vh.imgCollect.setImageResource(R.drawable.collect_select)
            }
        }
        vh.imgDan.setOnClickListener {
            val collectNum = vh.tvDz.text.toString().toInt()
            if (kv.decodeBool(vh.tvAuthor.text.toString() + "like", false)) {
                if (collectNum > 0) {
                    vh.tvDz.text = "${collectNum.dec()}"
                    kv.encode(vh.tvAuthor.text.toString() + "like", false)
                    vh.imgDan.setImageResource(R.drawable.dianzan)
                }
            } else {
                vh.tvDz.text = "${collectNum.inc()}"
                kv.encode(vh.tvAuthor.text.toString() + "like", true)
                vh.imgDan.setImageResource(R.drawable.dianzan_select)
            }
        }

        if (kv.decodeBool(vh.tvAuthor.text.toString() + "collect", false)) {
            val collectNum = videoEntity?.collect_num
            vh.tvCollect.text = "${collectNum?.inc()}"
            vh.imgCollect.setImageResource(R.drawable.collect_select)
        }
        if (kv.decodeBool(vh.tvAuthor.text.toString() + "like", false)) {
            val likeNum = videoEntity?.like_num
            vh.tvDz.text = "${likeNum?.inc()}"
            vh.imgDan.setImageResource(R.drawable.dianzan_select)
        }
    }

    override fun getItemCount(): Int {
        return if (videoData != null && videoData!!.isNotEmpty()) videoData!!.size else 0
    }
}